<?php

namespace Summer\Shop\Http\Controllers;

use Illuminate\Routing\Controller;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Db;
use Summer\Shop\Models\BrowseModel;

class BrowseController extends Controller
{
    use ApiReturn;

    /**
     * 浏览记录
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=BrowseModel::select();
        $query->with([
            'product'=>function($q){
                $q->with(['image_preview']);
            }
        ]);
        $query->where('user_id',$user->id);
        $records=$query->paginate($page_size,$page);
        return $this->apiSuccess('操作成功',$records);
    }
    /**
     * 删除浏览记录
     */
    public function delete(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $product_ids=Input::get('product_ids');
        if(!$product_ids){
            return $this->apiError('参数错误');
        }
        BrowseModel::where('user_id',$user->id)->whereIn('product_id',$product_ids)->delete();
        return $this->apiSuccess('操作成功',false);
    }

}
